package com.wfcm.utils;

import java.security.MessageDigest;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class WangsuSms {
    private RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(15000)
            .setConnectTimeout(15000)
            .setConnectionRequestTimeout(15000)
            .build();

    private static WangsuSms instance = null;
    private static int i = 3;
    private WangsuSms() {
    }

    public static WangsuSms getInstance() {
        if (instance == null) {
            instance = new WangsuSms();
        }
        return instance;
    }

    public String sendSms(String httpUrl, String telephone, String code) {
        HttpPost post = new HttpPost(httpUrl);
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("type", 2);
        params.put("content", "【网宿科技】您的验证码为 " + code + "。");
        params.put("phone", telephone);
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            int YY = c.get(Calendar.YEAR);
            int MM = c.get(Calendar.MONTH) + 1;
            int DD = c.get(Calendar.DATE);
            int HH = c.get(Calendar.HOUR_OF_DAY);
            int NN = c.get(Calendar.MINUTE);
            int SS = c.get(Calendar.SECOND);
            String timeStamp = "" + YY + (MM > 9 ? MM : "0" + MM) + (DD > 9 ? DD : "0" + DD)
                    + (HH > 9 ? HH : "0" + HH) + (NN > 9 ? NN : "0" + NN) + (SS > 9 ? SS : "0" + SS);
            System.out.println(timeStamp);
            post.setHeader("auth-user", "AKAX");
            post.setHeader("auth-timeStamp", timeStamp);
            String data = "auth-timeStamp=" + timeStamp + "&auth-user=AKAX&sms=" + new Gson().toJson(params) + "&userKey=ZFVZncuiwTFD9zWv";
            post.setHeader("auth-signature", MD5(data));
            StringEntity entity = new StringEntity("sms=" + new Gson().toJson(params), "UTF-8");
            entity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
            post.setEntity(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sendHttpPost(post);
    }

    private String MD5(String str) {
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(str.getBytes());
            StringBuffer buffer = new StringBuffer();
            for (byte b : result) {
                int number = b & 0xff;
                String s = Integer.toHexString(number);
                if (s.length() == 1) {
                    buffer.append("0");
                }
                buffer.append(s);
            }
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 发送Post请求
     *
     * @param httpPost
     * @return
     */
    private String sendHttpPost(HttpPost httpPost) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String content = null;
        try {
            httpClient = HttpClients.createDefault();
            httpPost.setConfig(requestConfig);
            response = httpClient.execute(httpPost);
            content = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return content;
    }

    public static void main(String[] args) {
//        String url = "https://sms.server.matocloud.com/sms/is/api/sms/simple/sendSms";
//        WangsuSms sms = WangsuSms.getInstance();
//        System.out.println(sms.sendSms(url, "18637328744", "489013"));
        System.out.println(i);
        i = 5;
        System.out.println(i);
    }


}